//A.2  inserted Date type from Chapter 9 with modification by mgm A.2
#include "Date.h"

//Date::Date(int yy, Month mm, int dd)
//:y(yy),m(mm), d(dd)
//{
//      if(!is_date(yy,mm,dd)) throw Invalid();
//}
//
//Date& Date::default_date()
//{
//      static Date dd(2010, Date::apr, 2); //turn in date for Project 1
//      return dd;
//}

Date::Date(int yy, int mm, int dd)
:y(yy),m(mm), d(dd)
{
        if(!is_date(yy,mm,dd)) throw Invalid();
}

Date& Date::default_date()
{
        static Date dd(2010, 4, 2); //turn in date for Project 1
        return dd;
}

Date::Date()
:y(default_date().year()),
m(default_date().month()),
d(default_date().day())
{
}

//bool Date::is_date(int y, Date::Month m, int d)
//{
//      //assume y is valid
//      if(d<=0) return false;
//      int days_in_month = 31; //assume month has 31 days
//      switch(m)
//      {
//      case Date::feb: //Feb is an exception
//              days_in_month = (leapyear(y))?29:28;
//              break;
//      case Date::apr: case Date::jun: case Date::sep: case Date::nov:
//              days_in_month = 30;
//              break;
//      }
//      if(days_in_month < d) return false;
//      return true;
//}

bool Date::is_date(int y, int m, int d)
{
        //assume y is valid
        if(d<=0) return false;
        int days_in_month = 31; //assume month has 31 days
        switch(m)
        {
        case 2: //Feb is an exception
                days_in_month = (leapyear(y))?29:28;
                break;
        case 4: case 6: case 9: case 11:
                days_in_month = 30;
                break;
        }
        if(days_in_month < d) return false;
        return true;
}

bool Date::leapyear(int y)
{
        //leapyear if divisible by 4, not divisible by 100, but is divisible by 400
        return (0 == y%4 && (0 != y%100 || 0 == y%400));
}

